package com.machuxin.course.patterns.single;

/**
 * 懒汉式
 */
public class Singleton2 {

    private volatile static  Singleton2 instance = null;

    private  String msg;

    /**
     * 私有化构造方法
     */
    private Singleton2() {
        msg = "初始化参数";
    }

    // 存在线程安全问题
    public static Singleton2 getInstance() {
        if (instance == null) {
            instance = new Singleton2();
        }
        return instance;
    }

    // 解决线程安全问题，但是效率低
    public synchronized static Singleton2 getInstance2() {
        if (instance == null) {
            instance = new Singleton2();
        }
        return instance;
    }

    // 解决线程安全问题，提升效率
    public static Singleton2 getInstance3() {
        if (instance == null) {
            synchronized (Singleton2.class){
                if(instance == null){
                    instance = new Singleton2();
                }
            }
        }
        return instance;
    }


}
